package com.example.zbh.dsliao.db;


import com.easemob.chat.EMMessage;
import com.example.zbh.dsliao.DlHelper;
import com.example.zbh.dsliao.db.entity.ApplyFriend;
import com.example.zbh.dsliao.db.entity.ApplyFriendDao;
import com.example.zbh.dsliao.db.entity.CarrouselPic;
import com.example.zbh.dsliao.db.entity.DaoSession;
import com.example.zbh.dsliao.db.entity.ForYouRecommend;
import com.example.zbh.dsliao.db.entity.HotSongMenu;
import com.example.zbh.dsliao.db.entity.ImageViewpager;
import com.example.zbh.dsliao.db.entity.ImageViewpagerDao;
import com.example.zbh.dsliao.db.entity.NewSong;
import com.example.zbh.dsliao.db.entity.NewSongImg;
import com.example.zbh.dsliao.db.entity.RadioSet;
import com.example.zbh.dsliao.db.entity.SongRecommend;
import com.example.zbh.dsliao.db.entity.UserContact;
import com.example.zbh.dsliao.db.entity.UserContactDao;

import java.util.List;

/**
 * Created by zbh on 15/11/25.
 */
public class DatabaseHelper {
    private static DatabaseHelper helper;
    private static DaoSession daoSession;


    public static DatabaseHelper getInstance(){
        if (helper == null){
            helper = new DatabaseHelper();
            daoSession = DlHelper.getDlHelper().getDaoSession();
        }
        return helper;
    }

    private DatabaseHelper(){

    }
    //保存申请和同意的联系人
    public void saveApplyFriend(ApplyFriend applyFriend){
        daoSession.getApplyFriendDao().insert(applyFriend);
    }

    //更新
   public void deleteFriendByName(String name){
       daoSession.getApplyFriendDao().queryBuilder().where(ApplyFriendDao.Properties.Name.eq(name)).buildDelete().executeDeleteWithoutDetachingEntities();
   }

    //获取数据库列表
    public List<ApplyFriend> queryFriend(){
        return daoSession.getApplyFriendDao().loadAll();
    }
    //清空数据库
    public void deleteFriend(){
        daoSession.getApplyFriendDao().deleteAll();
    }



    //轮播---添加数据
    public void saveCarousel(List<CarrouselPic>  carrouselPics){
        daoSession.getCarrouselPicDao().insertInTx(carrouselPics);
    }

    //轮播---查询所有
    public List<CarrouselPic> queryAllCarousel(){
        return daoSession.getCarrouselPicDao().loadAll();
    }

    //轮播---删除所有
    public void deleteAllCarousel(){
        daoSession.getCarrouselPicDao().deleteAll();
    }


    //为你推荐---添加数据
    public void saveForYouRecommend(List<ForYouRecommend> forYouRecommends){
        daoSession.getForYouRecommendDao().insertInTx(forYouRecommends);
    }


    //为你推荐---查询所有
    public List<ForYouRecommend> queryAllForYouRrcommend(){
        return daoSession.getForYouRecommendDao().loadAll();
    }


    //为你推荐---删除所有
    public void deleteAllForYouRrcommend(){
        daoSession.getForYouRecommendDao().deleteAll();
    }


    //热门歌单---添加数据
    public void saveHotSongMenu(List<HotSongMenu> hotSongMenus){
        daoSession.getHotSongMenuDao().insertInTx(hotSongMenus);
    }


    //热门歌单---查询所有
    public List<HotSongMenu> queryAllHotSongMenu(){
        return daoSession.getHotSongMenuDao().loadAll();
    }


    //热门歌单---删除所有
    public void deleteAllHotSongMenu(){
        daoSession.getHotSongMenuDao().deleteAll();
    }



    //歌曲推荐---添加数据
    public void saveSongRecommend(List<SongRecommend> songRecommends){
        daoSession.getSongRecommendDao().insertInTx(songRecommends);
    }


    //歌曲推荐---查询所有
    public List<SongRecommend> queryAllSongRecommend(){
        return daoSession.getSongRecommendDao().loadAll();
    }


    //歌曲推荐---删除所有
    public void deleteAllSongRecommend(){
        daoSession.getSongRecommendDao().deleteAll();
    }

    //最新歌曲图片---添加数据
    public void saveNewSongImg(NewSongImg newSongImgs){
        daoSession.getNewSongImgDao().insertInTx(newSongImgs);
    }


    //最新歌曲图片---查询所有
    public List<NewSongImg> queryAllNewSongImg(){
        return daoSession.getNewSongImgDao().loadAll();
    }


    //最新歌曲图片---删除所有
    public void deleteAllNewSongImg(){
        daoSession.getNewSongImgDao().deleteAll();
    }

    //最新歌曲---添加数据
    public void saveNewSong(List<NewSong> newSongs){
        daoSession.getNewSongDao().insertInTx(newSongs);
    }


    //最新歌曲---查询所有
    public List<NewSong> queryAllNewSong(){
        return daoSession.getNewSongDao().loadAll();
    }


    //最新歌曲---删除所有
    public void deleteAllNewSong(){
        daoSession.getNewSongDao().deleteAll();
    }


    //电台---添加所有
    public void saveRadioSet(List<RadioSet> radioSets){
        daoSession.getRadioSetDao().insertInTx(radioSets);
    }

    //电台---查询所有
    public List<RadioSet> queryAllRadioSet(){
        return daoSession.getRadioSetDao().loadAll();
    }


    //电台---删除所有
    public void deleteAllRadioSet(){
        daoSession.getRadioSetDao().deleteAll();
    }


    //联系人---添加所有
    public void saveContactPeople(List<UserContact> list){
        daoSession.getUserContactDao().insertInTx(list);
    }


    //根据用户名查询相应数组
    public UserContact queryContactPeopleByName(String name){
        List<UserContact> list = daoSession.getUserContactDao().queryBuilder().where(UserContactDao.Properties.Username.eq(name)).list();
        if (list !=null && list.size() > 0){
            return list.get(0);
        }
        return null;
    }


    //根据用户名查询相应数组
    public UserContact queryContactPeopleBynickName(String name){
        List<UserContact> list = daoSession.getUserContactDao().queryBuilder().where(UserContactDao.Properties.Nickname.eq(name)).list();
        if (list !=null && list.size() > 0){
            return list.get(0);
        }
        return null;
    }

    //删除联系人
    public void deleteContactPeople(){
        daoSession.getUserContactDao().deleteAll();
    }


    //保存一条图片消息
    public void saveImageUrl(ImageViewpager imageViewpager){
        daoSession.getImageViewpagerDao().insertInTx(imageViewpager);
    }
    //根据用户名查询全部的图片url 并返回集合
    public List<ImageViewpager> queryimageViewpagerAll(String name){
        List<ImageViewpager> list = daoSession.getImageViewpagerDao().queryBuilder().where(ImageViewpagerDao.Properties.Username.eq(name)).list();
        if (list != null && list.size() > 0){
            return list;
        }
        return null;
    }

    //删除图片数据库的内容
    public void deleteImageViewPager(){
        daoSession.getImageViewpagerDao().deleteAll();
    }



}
